package com.csw.mario;

import javazoom.jl.decoder.JavaLayerException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2022/11/6 15:21
 */
public class MyFrame extends JFrame implements KeyListener, Runnable {
    //用于储存所有的背景
    private List<BackGround> allBg = new ArrayList<>();
    //用于储存当前背景
    private BackGround nowBg = new BackGround();
    //双缓存
    private Image offScreen = null;
    //马里奥对象
    private Mario mario = new Mario();

    //线程对象实现马里奥运动
    private Thread thread = new Thread(this);

    @Override
    public void paint(Graphics g) {
        if (offScreen == null) {
            offScreen = createImage(800, 600);
        }
        Graphics graphics = offScreen.getGraphics();
        //对对象进行填充
        graphics.fillRect(0, 0, 800, 600);
        //绘制背景
        graphics.drawImage(nowBg.getBgImage(), 0, 0, this);
        //绘制敌人
//        nowBg.getEnemyList().forEach(enemy -> {
//            graphics.drawImage(enemy.getShow(), enemy.getX(), enemy.getY(), this);
//        });

        for (Enemy e : nowBg.getEnemyList()) {
            graphics.drawImage(e.getShow(), e.getX(), e.getY(), this);
        }
        //绘制障碍物
        for (Obstacle obstacle : nowBg.getObstacleList()) {
            graphics.drawImage(obstacle.getShow(), obstacle.getX(), obstacle.getY(), this);
        }
        //绘制城堡和旗杆
        graphics.drawImage(nowBg.getTower(), 620, 270, this);
        graphics.drawImage(nowBg.getGan(), 500, 220, this);
        //绘制马里奥
        graphics.drawImage(mario.getShow(), mario.getX(), mario.getY(), this);
        //绘制分数
        //Color color = graphics.getColor();
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("黑体", Font.BOLD, 25));
        graphics.drawString("当前分数:" + mario.getScore(), 300, 100);
        //graphics.setColor(color);

        //缓冲区图像绘制到窗口中中
        g.drawImage(offScreen, 0, 0, this);
    }

    //空参构造
    public MyFrame() {
        //设置窗口大小800*600
        this.setSize(800, 600);
        //窗口居中
        this.setLocationRelativeTo(null);
        //设置窗口可见
        this.setVisible(true);
        //设置窗口上的关闭键,结束程序
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗口大小不可变
        this.setResizable(false);
        //向窗口对象添加键盘监听器
        this.addKeyListener(this);
        //设置窗口名称
        this.setTitle("超级玛丽");
        //初始化图片
        StaticValue.init();
        //初始化马里奥对象
        mario = new Mario(10, 355);
        //创建所有的场景
        for (int i = 1; i <= 3; i++) {
            allBg.add(new BackGround(i, i == 3 ? true : false));
        }
        //将第一个场景作为当前背景
        nowBg = allBg.get(0);
        mario.setBackGround(nowBg);

        //绘制图像
        repaint();

        thread.start();
        //加载音乐
        try {
            new Music();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JavaLayerException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(StaticValue.path);
        MyFrame myFrame = new MyFrame();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //向右移动
        if (e.getKeyCode() == 39) {
            mario.rightMove();
        }
        //向左移动
        if (e.getKeyCode() == 37) {
            mario.leftMove();
        }
        //跳跃
        if (e.getKeyCode() == 38) {
            mario.jump();
        }
    }

    //键盘松开触发
    @Override
    public void keyReleased(KeyEvent e) {
        //向右停止
        if (e.getKeyCode() == 39) {
            mario.rightMove();
        }
        //向左停止
        if (e.getKeyCode() == 37) {
            mario.leftStop();
        }
    }

    @Override
    public void run() {
        while (true) {
            repaint();
            try {
                Thread.sleep(50);
                if (mario.getX() >= 775) {
                    //切换下一关场景
                    nowBg = allBg.get(nowBg.getSort());
                    mario.setBackGround(nowBg);
                    //下一关初始位置
                    mario.setX(10);
                    mario.setY(355);
                }
                //马里奥死亡
                if (mario.isDeath()) {
                    JOptionPane.showMessageDialog(this, "GG");
                    System.exit(0);
                }

                //走到了城堡门口
                if (mario.isOK()) {
                    JOptionPane.showMessageDialog(this, "恭喜你成功通关");
                    System.exit(0);
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
